/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tp3.facade;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import tp3.entities.CompteBancaire;
import tp3.entities.OperationBancaire;

/**
 *
 * @author Amaury
 */
@Stateless
public class CompteBancaireFacade extends AbstractFacade<CompteBancaire> {
    @PersistenceContext(unitName = "TP3BauzacDonnadei2-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CompteBancaireFacade() {
        super(CompteBancaire.class);
    }
    public void creerComptesTest() {
            System.out.println("creation des comptes...");
       create(new CompteBancaire("John Lennon", Double.valueOf(150000)));  
       create(new CompteBancaire("Cartney", Double.valueOf(950000)));  
       create(new CompteBancaire("Ringo Starr", Double.valueOf(20000)));  
       create(new CompteBancaire("Georges Harrisson", Double.valueOf(100000)));  
       create(new CompteBancaire("John Lennon", Double.valueOf(150000)));  
       create(new CompteBancaire("Cartney", Double.valueOf(950000)));  
       create(new CompteBancaire("Ringo Starr", Double.valueOf(20000)));  
       create(new CompteBancaire("Georges Harrisson", Double.valueOf(100000)));  
       create(new CompteBancaire("John Lennon", Double.valueOf(150000)));  
       create(new CompteBancaire("Cartney", Double.valueOf(950000)));  
       create(new CompteBancaire("Ringo Starr", Double.valueOf(20000)));  
       create(new CompteBancaire("Georges Harrisson", Double.valueOf(100000)));  
       create(new CompteBancaire("John Lennon", Double.valueOf(150000)));  
       create(new CompteBancaire("Cartney", Double.valueOf(950000)));  
       create(new CompteBancaire("Ringo Starr", Double.valueOf(20000)));  
       create(new CompteBancaire("Georges Harrisson", Double.valueOf(100000)));  
       create(new CompteBancaire("John Lennon", Double.valueOf(150000)));  
       create(new CompteBancaire("Cartney", Double.valueOf(950000)));  
       create(new CompteBancaire("Ringo Starr", Double.valueOf(20000)));  
       create(new CompteBancaire("Georges Harrisson", Double.valueOf(100000)));  
       create(new CompteBancaire("John Lennon", Double.valueOf(150000)));  
       create(new CompteBancaire("Cartney", Double.valueOf(950000)));  
       create(new CompteBancaire("Ringo Starr", Double.valueOf(20000)));  
       create(new CompteBancaire("Georges Harrisson", Double.valueOf(100000)));  
       create(new CompteBancaire("John Lennon", Double.valueOf(150000)));  
       create(new CompteBancaire("Cartney", Double.valueOf(950000)));  
       create(new CompteBancaire("Ringo Starr", Double.valueOf(20000)));  
       create(new CompteBancaire("Georges Harrisson", Double.valueOf(100000)));  
       create(new CompteBancaire("John Lennon", Double.valueOf(150000)));  
       create(new CompteBancaire("Cartney", Double.valueOf(950000)));  
       create(new CompteBancaire("Ringo Starr", Double.valueOf(20000)));  
       create(new CompteBancaire("Georges Harrisson", Double.valueOf(100000)));  
       create(new CompteBancaire("John Lennon", Double.valueOf(150000)));  
       create(new CompteBancaire("Cartney", Double.valueOf(950000)));  
       create(new CompteBancaire("Ringo Starr", Double.valueOf(20000)));  
       create(new CompteBancaire("Georges Harrisson", Double.valueOf(100000)));  
       create(new CompteBancaire("John Lennon", Double.valueOf(150000)));  
       create(new CompteBancaire("Cartney", Double.valueOf(950000)));  
       create(new CompteBancaire("Ringo Starr", Double.valueOf(20000)));  
       create(new CompteBancaire("Georges Harrisson", Double.valueOf(100000)));  
       create(new CompteBancaire("John Lennon", Double.valueOf(150000)));  
       create(new CompteBancaire("Cartney", Double.valueOf(950000)));  
       create(new CompteBancaire("Ringo Starr", Double.valueOf(20000)));  
       create(new CompteBancaire("Georges Harrisson", Double.valueOf(100000)));  
       create(new CompteBancaire("John Lennon", Double.valueOf(150000)));  
       create(new CompteBancaire("Cartney", Double.valueOf(950000)));  
       create(new CompteBancaire("Ringo Starr", Double.valueOf(20000)));  
       create(new CompteBancaire("Georges Harrisson", Double.valueOf(100000)));  
       
      
    }
    
    public void transfer(CompteBancaire debiteur, CompteBancaire crediteur, double montant){
        debiteur.setSolde(debiteur.getSolde()-montant);
        debiteur.addOperationBancaire(new OperationBancaire("Transfert débité", montant*-1));
        crediteur.setSolde(crediteur.getSolde()+montant);
        crediteur.addOperationBancaire(new OperationBancaire("Transfert crédité", montant));
        // methode edit = update dans les facades
        edit(debiteur);
        edit(crediteur);
    }
    
    public void doRetraitDepot(CompteBancaire compte, Double montant){
        //System.out.println(compte.getSolde() + " montant : " + montant);
        compte.setSolde(compte.getSolde()+montant);
        edit(compte);
        
    }
    /**
     * Utiliser pour affiche_comptes par primefaces
     * @param start = début de la liste
     * @param nbComptes = nombre de comptes affichés
     */
    public List<CompteBancaire> getLazyComptes(int start, int nbComptes){
        
        Query q = em.createNamedQuery("CompteBancaire.findAll");
        q.setFirstResult(start);
        q.setMaxResults(nbComptes);
        
        System.out.println("EJB ---- GETLAZY");
        return q.getResultList();
    }
    
    public List<CompteBancaire> getComptesContaining(String q){
        
        Query query = em.createQuery("Select c from CompteBancaire c where upper(c.proprietaire) like upper('%" + q +"%')");
        
        return query.getResultList();
    }
    
}
